package com.nxk.modules.core.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nxk.modules.core.entity.BaseEntity;
import com.nxk.modules.core.entity.SysRole;
import com.nxk.modules.core.service.SysRoleService;
import com.nxk.modules.exception.ResultEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/a/core/role")
public class SysRoleController {
    
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 保存数据接口
     * @param sysRole
     * @return
     */
    @RequiresPermissions("core:role:edit")
    @PutMapping("/save")
    public ResultEntity save(@Validated SysRole sysRole) {
        QueryWrapper<SysRole> eq = new QueryWrapper<SysRole>()
                .eq("role_code", sysRole.getRoleCode())
                .eq("status",BaseEntity.STATUS_NORMAL);
        if (sysRoleService.getOne(eq) == null) {
            boolean save = sysRoleService.save(sysRole);
            return save ? ResultEntity.success(sysRoleService.getById(sysRole.getId())) : ResultEntity.fail();
        }
        return ResultEntity.fail("此角色编码已存在");
    }


    /**
     * 更新数据接口
     * @param sysRole
     * @return
     */
    @RequiresPermissions("core:role:edit")
    @PostMapping("/update")
    public ResultEntity update(@Validated SysRole sysRole) {
        QueryWrapper<SysRole> eq = new QueryWrapper<SysRole>()
                .eq("role_code", sysRole.getRoleCode())
                .notIn("id",sysRole.getId())
                .eq("status",BaseEntity.STATUS_NORMAL);
        if (sysRoleService.getOne(eq) == null) {
            boolean save = sysRoleService.save(sysRole);
            return save ? ResultEntity.success(sysRoleService.getById(sysRole.getId())) : ResultEntity.fail();
        }
        return ResultEntity.fail("此角色编码已存在");
    }


    /**
     * 修改数据状态接口
     * @param sysRole
     * @return
     */
    @RequiresPermissions("core:role:edit")
    @PostMapping("/updateStatus")
    public ResultEntity updateStatus(SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);
        return save ? ResultEntity.success(sysRoleService.getById(sysRole.getId())) : ResultEntity.fail();
    }


    /**
     * 根据id获取单条数据接口
     * @param sysRole
     * @return
     */
    @RequiresPermissions("core:role:view")
    @GetMapping("/get")
    public ResultEntity get(SysRole sysRole) {
        return ResultEntity.success(sysRoleService.getById(sysRole.getId()));
    }


    /**
     * 不分页查询所有数据接口
     * @return
     */
    @RequiresPermissions("core:role:view")
    @PostMapping("listAll")
    public ResultEntity listAll() {
        return ResultEntity.success(sysRoleService.ListAll());
    }

    /**
     * 查询分页数据接口
     * @param sysRole
     * @return
     */
    @RequiresPermissions("core:role:view")
    @PostMapping("list")
    public ResultEntity list(SysRole sysRole) {
        return ResultEntity.success(sysRoleService.page(sysRole));
    }


    /**
     * 根据id物理删除数据接口
     * @param sysRole
     * @return
     */
    @RequiresPermissions("core:role:edit")
    @DeleteMapping("delete")
    public ResultEntity delete(SysRole sysRole) {
        boolean delete = sysRoleService.deleteById(sysRole.getId());
        return delete ? ResultEntity.success() : ResultEntity.fail();
    }
    
}
